﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DispatchAssist.Service;

namespace DispatchAssist.AppHost
{
    /// <summary>
    /// 用户身份认证上下文
    /// </summary>
    public class AuthContext
    {
        #region Ctor.

        private AuthContext()
        {
        }

        #endregion

        #region Properties
        static AuthService proxy = new AuthService();

        public static AuthContext Current;

        public SeatUser User { get; set; }

        public SeatRole Role { get; set; }

        public List<Credential> Credentials { get; set; }

        public Credential SelfSeat
        {
            get 
            {
               return Credentials.Find(f => f.CredentialType == CredentialType.Self);
            }
        }

        public List<Credential> DeletegateSeats
        {
            get
            {
                var item = Credentials.Where(f=>f.CredentialType==CredentialType.Delegate);

                if (item == null)
                {
                    return new List<Credential>();
                }
                else
                {
                    return item.ToList();
                }
            }
        }

        public Guid[] DelegateSeatIDs
        {
            get
            {
               return DeletegateSeats.ToDictionary<Credential, Guid>(f => f.SeatID).Keys.ToArray();
            }
        }

        #endregion

        #region Methods

        public static bool Login(string userName, string password, Seat seat)
        {
            SeatUser user = null;

            byte[] pwd = CarolLib.EncryptHelper.GetPassword(password);

            user = proxy.Login(userName, pwd, seat.SeatID);

            if (user != null)
            {
                AuthContext context = new AuthContext();
                context.User = user;

                context.Role = proxy.GetRoleByID(user.RoleID);


                context.Credentials = new List<Credential>();
                context.Credentials.Add(new Credential(seat, CredentialType.Self));

                AuthContext.Current = context;
                proxy.DASoapHeaderValue = context.GetSoapHeader();

                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Logout()
        {
            Application.Exit();
            return true;
        }

        public bool Logout(Guid SeatID)
        {
            var cred = this.Credentials.Find(c => c.SeatID == SeatID);

            if (cred != null)
            {
                AuthService proxy = new AuthService();

                bool result = proxy.Logout(this.User.UserID, cred.SeatID);

                this.Credentials.Remove(cred);
                if (this.Credentials.Count == 0)
                {
                    Application.Exit();
                }

                return true;
            }
            else
            {
                return false;
            }
        }

        public void RequestDelegate(string SeatName)
        {

        }

        public DASoapHeader GetSoapHeader()
        {
            DASoapHeader header = new DASoapHeader();
            header.UserId = this.User.UserID.ToString();
           // header.RoleId = this.Role.RoleID.ToString();
            header.RoleId = this.User.RoleID.ToString();
            header.SeatId = this.Credentials.Select(c => c.SeatID.ToString()).ToArray();

            header.SelfSeatID = this.Credentials.Where(f => f.CredentialType == CredentialType.Self).Select(c => c.SeatID.ToString()).ElementAt(0);

            header.DelegateSeatIDs = this.Credentials.Where(f=>f.CredentialType==CredentialType.Delegate).Select(c=>c.SeatID.ToString()).ToArray();

            return header;
        }

        #endregion
    }

    /// <summary>
    /// 授权信息
    /// </summary>
    public class Credential
    {
        public Credential(Seat seat, CredentialType type)
        {
            this.CredentialSeat = seat;
            this.CredentialType = type;
        }

        protected Seat CredentialSeat { get; set; }

        public CredentialType CredentialType { get; set; }
        public Guid SeatID
        {
            get
            {
                return this.CredentialSeat.SeatID;
            }
        }
        public string SeatName
        {
            get
            {
                return this.CredentialSeat.SeatName;
            }
        }

    }



    /// <summary>
    /// 授权凭证类型
    /// </summary>
    public enum CredentialType
    {
        Self = 1,
        Delegate = 2
    }



}
